package main

import (
	"fmt"
	"runtime"
)

var quit chan int = make(chan int)

func print10to19( split_index int) {
	fmt.Println("Start******print10to19 ")
	for i := 0; i <= split_index; i++ {
		// 显式地让出CPU时间给其他goroutine
		runtime.Gosched()
		fmt.Println("******10to19: ", i)
	}
	fmt.Println("End******print10to19 ")
	quit <- 1
}

func print20to29(split_index int ) {
	fmt.Println("Start======print20to29 ")
	for i := split_index ; i <= 43; i++ {
		// 显式地让出CPU时间给其他goroutine
		runtime.Gosched()
		fmt.Println("======20to29: ", i)
	}
	fmt.Println("End======print20to29 ")
	quit <- 2
}

func print30to39() {
	fmt.Println("Start######print30to39 ")
	for i := 30; i < 40; i++ {
		// 显式地让出CPU时间给其他goroutine
		runtime.Gosched()
		fmt.Println("######30to39: ", i)
	}
	fmt.Println("End######print30to39 ")
	quit <- 0
}

func main() {
	// 设置最大开n个原生线程
	runtime.GOMAXPROCS(3)

	fmt.Println("start ---")

	split_index := split_token()

	go print10to19(split_index)
	go print20to29(split_index + 1)
	// go print30to39()

	//  go print10to19(begin1,end1)
	//  go print20to29(begin2,end2)
	fmt.Println("start ===")
	for i := 0; i < 2; i++ {
		sc := <-quit
		fmt.Println("sc:", sc)
	}

	fmt.Println("end")
}


// 拆分设备组
func split_token() int{

	// redisMaster := midware.NewPRedis(config.Config.GetString("REDIS_MASTER"))
	// group_len, err := redisMaster.ZCard(str.GID)        // 获取redis组的设备总数

	// 测试：拆分成2个协程
	// split_task(2,0,group_len)
	res := split_task(2,0,43)
	return res
}


func split_task(N int, WholeBegin int, WholeEnd int) int{

	res := (WholeEnd - WholeBegin) / N

	switch res {
	case 0:
		fmt.Printf("0....%s",WholeEnd)
	default:
		fmt.Printf("Default012", res)
	}

	return res
}